Repository navigation
fix: honour Accept-Encoding quality values and set Vary - #28
Merged
Merged
Conversation
`Accept-Encoding` was matched by substring, so quality values were ignored: `br;q=0, gzip` still picked brotli, `gzip;q=0` still compressed with gzip and `gzip;q=0.1, deflate;q=0.9` picked gzip. The header is now parsed per RFC 9110: the client's quality values decide, `q=0` rejects a coding, `*` covers unlisted codings and `x-gzip` counts as gzip. Server preference only breaks ties. Every negotiated response now gets `Vary: Accept-Encoding`, compressed or not, keeping existing `Vary` values (and updating the `render:response` header record in place), so shared caches no longer hand a compressed body to clients that didn't ask for it. `compress()` / `compressStream()` negotiate when `method` is omitted, which lets `useCompression*` and the Nuxt module set `Vary` even when no encoding is acceptable. Empty bodies are skipped on all paths, which also stops the middlewares from crashing on 204 responses and from gzipping empty bodies. Closes #25
This was referenced Nov 24, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #25
What
1. RFC 9110
Accept-EncodingparsingAccept-Encodingused to be matched withheader.includes(method), so quality values were ignored entirely. It is now parsed per RFC 9110 §12.5.3:Accept-Encodingbr;q=0, gzipbrgzipgzip;q=0gzipgzip;q=0.1, deflate;q=0.9gzipdeflatebr;q=0, *gzipx-gzipgzipdeflate-rawdeflateq=0(or a malformedq) rejects a coding.*covers every coding not listed; coding names are case-insensitive.The forced composables (
useGZipCompression, …) andcompress(event, response, 'gzip')use the same check, so a forced coding the client rejected withq=0is no longer applied.2.
Vary: Accept-EncodingEvery negotiated response now gets
Vary: Accept-Encoding— compressed or not, since anotherAccept-Encodingwould have changed the outcome either way. Existing values are kept (Origin→Origin, Accept-Encoding), it's never duplicated andVary: *is left alone. On the hook path aVaryin therender:responseheader record is updated in place, because Nitro applies that record after the hook.Not varied: already-encoded responses, empty bodies, bodies below
minSize/threshold, and a method forced on the middleware — that one is documented as "always", ignoresAccept-Encoding, and stays that way.3. Plumbing
compress()/compressStream()takemethodas optional now and negotiate when it's omitted (the middleware helpers already worked that way).useCompression*and the Nuxt module go through that, which is what lets them setVarywhen no coding is acceptable.compressStream()also gained theoptionsparameter. Both are backwards compatible.Side fixes found while testing
compression()middleware crashed on 204 responses (TypeError: Response constructor: Invalid response status code 204), andcompressionStream()gzipped the empty body h3 v2 produces for anullhandler result.compressStream()crashed on anundefinedbody (Cannot read properties of null (reading 'pipeThrough')).compressStream()with a rejected coding no longer replaces the body with a stream.Tests
New
test/negotiation.test.ts(31 of its tests fail onmain):getAnyCompression/getStreamCompressionincl. wildcard, aliases, malformedq, case/whitespace, zstd;Varyon the hook path (compressed, rejected, existing values,*,render:responserecord, encoded/empty/small → untouched, stream path, Nuxt plugin);Varypreservation, forced method,nulland 204 handlers;useCompression,useCompressionStreamand the Nuxt plugin.Full suite green locally on h3 2.0.8 and 1.15.11. README gained a "Content negotiation" section.